'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 宝箱
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var propertyType = require('../../../consts/propertyType');
var PlayerManager = require('../../../libs/playerManager');
var dataApi = require('../../../libs/dataApi');
var randomUtil = require('../../../util/randomUtil');
var rewardService = require('../../../services/rewardService');
var genRewardService = require('../../../services/genRewardService');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 打开宝箱
 */
Handler.prototype.use = function (msg, session, next) {
    var role_id = session.uid;
    var property_id = msg.property_id;

    if (!property_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'build', 'soldier', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var bag_model = player_manager.bag;

                // 判断背包是否存在宝箱
                var property_item = bag_model.getProperty(property_id);
                if (!property_item) {
                    return next(null, {
                        code: Code.BAG_ITEM_NOT_EXIST
                    });
                }

                var reward;

                // 判断宝箱类型: 固定或随机
                if (_getChestType(property_item.property_type) != propertyType.CHEST) {
                    reward = _getRandomChestReward(property_item.property_type);
                    rewardService.addRewardToRole(player_manager, reward, moduleType.MDL_OTHER, operationType.OPR_OTHER_OPEN_NEXUS_BOX);
                } else {
                    reward = _getChestReward(property_item.property_type);
                    rewardService.addRewardListToRole(player_manager, reward, moduleType.MDL_OTHER, operationType.OPR_OTHER_OPEN_NEXUS_BOX);
                }

                bag_model.removeProperty(property_id);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

/**
 * 宝箱类型
 */
var _getChestType = function (property_type) {
    var chest_conf = dataApi.property.findById(property_type);
    return chest_conf.type;
};

/**
 * 宝箱奖励
 */
var _getRandomChestReward = function (property_type) {
    var chest_reward_conf = dataApi.property_random.findById(property_type);
    return genRewardService.generateReward(randomUtil.sample(chest_reward_conf));

};
/**
 * 固定宝箱奖励
 */
var _getChestReward = function (property_type) {
    var reward = [];
    var chest_reward_conf = dataApi.property_box.findById(property_type);
    _.each(chest_reward_conf, function (chest_reward_item) {
        reward.push(genRewardService.generateReward(chest_reward_item));
    });
    return reward;
};
